<template>
  <div class="vacation-rules-container">
    <div class="page-header">
      <div>
        <el-button type="info" @click="handleAdd"> 假期规则初始化 </el-button>
      </div>
    </div>
    <el-table
      v-loading="vacationStore.loading"
      :data="vacationStore.rulesList"
      border
      
      style="width: 100%"
    >
      <el-table-column label="假期名称" min-width="120" prop="leaveName" />
      <el-table-column label="请假单位" min-width="120" prop="leaveViewUnit">
        <template #default="{ row }">
          {{ leaveViewUnitMap[row.leaveViewUnit] || "未知单位" }}
        </template>
      </el-table-column>

      <el-table-column label="余额规则" min-width="120" prop="balanceRule">
        <template #default="{ row }">
          <span
            v-if="row.salary_type == '1'"
            @click="PaidRules(row)"
            style="cursor: pointer; color: #56bc5b"
            >全薪</span
          >
          <span
            v-else-if="row.salary_type == '2'"
            @click="PaidRules(row)"
            style="cursor: pointer; color: #e6a23c"
            >4/5薪</span
          >
          <span v-else @click="PaidRules(row)" style="cursor: pointer; color: #c0c4cc"
            >无薪</span
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="centerDialogVisible" title="带薪类型" width="350" align-center>
      <div style="margin-top: 20px">
        <el-radio-group v-model="forndata.salary_type">
          <el-radio-button label="无薪" value="3" />
          <el-radio-button label="4/5薪" value="2" />
          <el-radio-button label="全薪" value="1" />
        </el-radio-group>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submittType"> 保存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref, nextTick } from "vue";
import { ElMessage } from "element-plus";
import { useVacationStore } from "@/stores/vacation";

const vacationStore = useVacationStore();

//修改提示框
const centerDialogVisible = ref(false);

import { useDepartmentStore } from "@/stores/department";

const departmentStore = useDepartmentStore();

import { Vacationbone, updateVacationRule } from "@/api/vacation";

// 请假时长单位映射
const leaveViewUnitMap = {
  day: "按天请假",
  halfDay: "按半天请假",
  hour: "按小时请假",
};

const forndata = reactive({
  id: "",
  leaveCode: "",
  salary_type: "3",
});

//修改按钮
const PaidRules = (row: any) => {
  Object.assign(forndata, {
    id: "",
    leaveCode: "",
    salary_type: "3",
  });
  if (row.salary_type) {
    forndata.salary_type = `${row.salary_type}`;
  }

  forndata.leaveCode = row.leaveCode;
  forndata.id = row.id;
  centerDialogVisible.value = true;
};

const submittType = async () => {
  try {
    const res = await updateVacationRule(forndata);
    if (res.code == "200") {
      vacationStore.fetchRulesList();
    }
  } catch (error) {
    console.error(error);
  }

  centerDialogVisible.value = false;
};

// 假期初始化
const handleAdd = async () => {
  try {
    const res = await Vacationbone();
    if (res.code == "200") {
      ElMessage.success("假期规则初始化成功");
      await departmentStore.fetchDepartmentList();
      vacationStore.fetchRulesList();
    } else {
      ElMessage.error("假期规则初始化失败");
    }
  } catch (error) {
    console.error("假期规则初始化失败");
  }
};

// 初始化数据
onMounted(async () => {
  await departmentStore.fetchDepartmentList();
  vacationStore.fetchRulesList();
});
</script>
<style lang="scss" scoped>
.vacation-rules-container {
  padding: 20px;
  background: #fff;
  border-radius: 4px;

  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    h2 {
      margin: 0;
      font-size: 18px;
      font-weight: 600;
    }
  }

  :deep(.el-button) {
    .el-icon {
      margin-right: 4px;
    }
  }
}

.form-section {
  margin-bottom: 24px;
  padding: 16px;
  background-color: #f5f7fa;
  border-radius: 4px;

  .section-title {
    font-size: 14px;
    font-weight: 500;
    color: #303133;
    margin-bottom: 16px;
  }

  .section-tip {
    font-size: 12px;
    color: #909399;
    margin: -8px 0 16px 120px;
  }

  :deep(.el-form-item:last-child) {
    margin-bottom: 0;
  }
}
</style>
